<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    {{dataForm}}
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="模板名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="模板名称"></el-input>
    </el-form-item>
      <el-form-item label="关联品牌" prop="brandIds">
        <!--<el-input v-model="dataForm.brandIds" placeholder="关联品牌"></el-input>-->
        <el-select v-model="dataForm.brandIds" multiple placeholder="请选择关联品牌" value-key="id">
          <el-option
            v-for="brand in brandList"
            :key="brand.id"
            :label="brand.text"
            :value="brand">
          </el-option>
        </el-select>
      </el-form-item>
    <el-form-item label="关联规格" prop="specIds">
      <!--<el-input v-model="dataForm.specIds" placeholder="关联规格"></el-input>-->
      <el-select v-model="dataForm.specIds" multiple placeholder="请选择关联规格" value-key="id">
        <el-option
          v-for="spec in specList"
          :key="spec.id"
          :label="spec.text"
          :value="spec">
        </el-option>
      </el-select>
    </el-form-item>

    <el-form-item label="扩展属性" prop="customAttributeItems">
      <!--<el-input v-model="dataForm.customAttributeItems" placeholder="自定义属性"></el-input>-->
      <el-button type="primary" plain size="small" @click="dataForm.customAttributeItems.push({})">新增扩展属性</el-button>
      <el-table
        :data="dataForm.customAttributeItems"
        style="width: 100%">
        <el-table-column
          label="属性名称"
          align="center"
          width="400">
          <template slot-scope="scope">
            <el-input v-model="scope.row.text"></el-input>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="200"  align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="danger"
              @click="dataForm.customAttributeItems.splice(index,1)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          specIds: '',
          brandIds: '',
          customAttributeItems: []
        },
        brandList:[],   // 品牌列表
        specList:[],    // 规格列表
        dataRule: {
          name: [
            { required: true, message: '模板名称不能为空', trigger: 'blur' }
          ],
          specIds: [
            { required: true, message: '关联规格不能为空', trigger: 'blur' }
          ],
          brandIds: [
            { required: true, message: '关联品牌不能为空', trigger: 'blur' }
          ],
          customAttributeItems: [
            { required: true, message: '自定义属性不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    created() {
      //1. 查询所有品牌
      this.findBrandList()
      //2. 查询所有规格
      this.findSpecList()
    },
    methods: {
      findBrandList(){
        this.$http({
          url: this.$http.adornUrl(`/manager/brand/findAll`),
          method: 'get'
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.brandList = data.list;
          }
        })
      },
      findSpecList(){
        this.$http({
          url: this.$http.adornUrl(`/manager/specification/findAll`),
          method: 'get'
        }).then(({data}) => {
          if (data && data.code === 0) {
            //处理规格中的specName字段为text字段  [{'id':xxx,'text':xxxx}]
            this.specList = data.list.map(m=>{
              return {"id":m.id,"text":m.specName}
            })
          }
        })
      },
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/manager/typetemplate/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.typeTemplate.name
                this.dataForm.specIds = JSON.parse(data.typeTemplate.specIds).map(m=>{
                  return {"id":m.id + "","text":m.text}
                })
                this.dataForm.brandIds = JSON.parse(data.typeTemplate.brandIds).map(m=>{
                  return {"id":m.id + "","text":m.text}
                })
                this.dataForm.customAttributeItems = JSON.parse(data.typeTemplate.customAttributeItems)
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/manager/typetemplate/${!this.dataForm.id ? 'save' : 'update'}`),
              method: `${this.dataForm.id ? 'PUT' : 'POST'}`,
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'specIds': JSON.stringify(this.dataForm.specIds),
                'brandIds': JSON.stringify(this.dataForm.brandIds),
                'customAttributeItems': JSON.stringify(this.dataForm.customAttributeItems)
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
